package com.jinglan.hlht.api.impl;

import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.jinglan.framework.common.OrderState;
import com.jinglan.framework.util.string.StringUtil;
import com.jinglan.hlht.api.QbHlhtChannel;
import com.jinglan.hlht.service.HlhtService;
import com.jinglan.qcoin.center.pojo.QcoinChannelOrder;
/**
 * 
 * @author 测试程序的实现 Created by 肖硕博 on 2017/3/13.
 */
@Service
public class QbHlhtChannelImpl implements QbHlhtChannel,InitializingBean{
	private Logger logger= LoggerFactory.getLogger(QbHlhtChannelImpl.class);
	private Random random=new Random();
	@Autowired
	private HlhtService hlhtService;
	@Override//下单方法
	public QcoinChannelOrder placeOrder(boolean testEnv, Integer testOrderState, QcoinChannelOrder channelOrder) {
		String orderNo=channelOrder.getOrderNo();
		String channelOrderNo=channelOrder.getChannelOrderNo();
		long rechargeAmount=channelOrder.getRechargeAmount();
		String number=channelOrder.getQq();
		String msgHead="系统订单["+orderNo+"]渠道订单["+channelOrderNo+"]充值账号["+number+"]";
       //fixedip与ip的优先使用
		String ip= StringUtil.isNull(channelOrder.getFixedIp()) ? channelOrder.getIp():channelOrder.getFixedIp();
		QcoinChannelOrder rstChannelOrder =null;
		if(testEnv){
			rstChannelOrder=new QcoinChannelOrder();
			rstChannelOrder.setChannelOrderNo(channelOrderNo);
			rstChannelOrder.setOrderState(OrderState.handling.getState());
			rstChannelOrder.setOrderStateDesc(OrderState.handling.getStateDesc());
			rstChannelOrder.setChannelResult("testChannelResult");
			rstChannelOrder.setChannelState("testChannelState");
		}else{
			try{
			rstChannelOrder= hlhtService.placeOrder(msgHead,channelOrderNo,rechargeAmount,number,ip);
			} catch (Exception e) {
				logger.info(msgHead + "下单异常" + e);
			}
		}
		logger.info(msgHead+"当前[测试]渠道["+(testEnv?"打桩":"生产")+"]环境[下单]返回结果:"+ JSON.toJSONString(rstChannelOrder));
		return rstChannelOrder;
	}

	@Override//查单
	public QcoinChannelOrder queryOrder(boolean testEnv, Integer testOrderState, QcoinChannelOrder channelOrder) {
		String orderNo = channelOrder.getOrderNo();
		String channelOrderNo = channelOrder.getChannelOrderNo();

		String number= channelOrder.getQq();
		String msgHead = "系统订单[" + orderNo + "]渠道订单[" + channelOrderNo + "]查询号码[" + number + "]";

		QcoinChannelOrder rstChannelOrder;
		if (testEnv) {
			rstChannelOrder = new QcoinChannelOrder();
			rstChannelOrder.setChannelOrderNo(channelOrderNo);
			rstChannelOrder.setChannelResult("testChannelResult");
			rstChannelOrder.setChannelState("testChannelState");
			if (testOrderState == null) {
				testOrderState = random.nextInt(3);
			}
			switch (testOrderState) {
			case 0:
				rstChannelOrder.setOrderState(OrderState.fail.getState());
				rstChannelOrder.setOrderStateDesc(OrderState.fail.getStateDesc());
				break;
			case 1:
				rstChannelOrder.setOrderState(OrderState.success.getState());
				rstChannelOrder.setOrderStateDesc(OrderState.success.getStateDesc());
				break;
			default:
				rstChannelOrder.setOrderState(OrderState.unsure.getState());
				rstChannelOrder.setOrderStateDesc(OrderState.unsure.getStateDesc());
				break;
			}
		} else {
			rstChannelOrder = hlhtService.queryOrder(msgHead, number, channelOrderNo,
					channelOrder.getPartnerOrderNo());
		}
		logger.info(
				msgHead + "当前[测试]渠道[" + (testEnv ? "打桩" : "生产") + "]环境[查单]返回结果:" + JSON.toJSONString(rstChannelOrder));
		return rstChannelOrder;
	}

	@Override
	public String queryBalance(boolean testEnv) {
		
		if (testEnv) {
			return null;
		}
		return hlhtService.queryBalance();
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		QcoinChannelOrder qcoinChannelOrder=new QcoinChannelOrder();
		qcoinChannelOrder.setChannelOrderNo("1235567902244");
		Integer testOrderState=1;
		queryOrder(false, testOrderState, qcoinChannelOrder);
		System.out.println("11111111111");
		
	}

}
